<?php

class Visitor extends CI_Controller {
	
	private $avatar;
	
	function Visitor() {
		parent::__construct ();
		$username = $this->session->userdata('username');
		$level = $this->session->userdata('level');
		if($username !='' && $level == 1 ){
			
		}
		else
		{
			redirect(base_url().'index.php/user');
		}
		$this->load->model ( 'be/visitor_model', 'model' );
		$this->load->library ( 'form_validation' );
		$this->load->helper ( array ('form', 'url' ) );
	}
	
	function index() {
		$this->list_all_visitors ();
	}
	
	function add() {
		$this->form_validation->set_rules ( 'fullname', 'Họ tên','required');
		//$this->form_validation->set_rules ( 'username', 'Tên tài khoản', 'required|min_lenght[5]|max_lenght[20]|is_unique[visitors.username]');
		//$this->form_validation->set_rules ( 'password', 'Mật khẩu', 'required|matches[password]' );
		$this->form_validation->set_rules ( 'email', 'Email', 'required|valid_email|is_unique[visitors.email]' );
		$this->form_validation->set_rules ( 'phone', 'Điện thoại','required' );
		$this->form_validation->set_rules ( 'address', 'Địa chỉ' , 'required' );
		$this->form_validation->set_rules ( 'district', 'Quận/Huyện', 'required' );
		$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố', 'required' );
		$this->form_validation->set_rules ( 'active_status', 'Trạng thái', 'required' );
		$this->form_validation->set_rules ( 'avatar', 'Hình ảnh', 'callback_get_avatar_uploaded' );
		$this->form_validation->set_rules ( 'group', 'Nhóm khách hàng', 'required' );
		$this->form_validation->set_rules ( 'description', 'Mô tả' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['page_title'] = 'Thêm mới khách vãng lai';
			$data ['main_content'] = 'be/visitor/add';
			$this->load->view ( 'be/includes/template', $data );
		} else {
			$data ['name'] = $this->input->post ( 'fullname' );
//			$data ['username'] = $this->input->post ( 'username' );
//			$data ['password'] = $this->input->post ( 'password' );
			$data ['email'] = $this->input->post ( 'email' );
			$data ['phone'] = $this->input->post ( 'phone' );
			$data ['address'] = $this->input->post ( 'address' );
			$data ['district'] = $this->input->post ( 'district' );
			$data ['province'] = $this->input->post ( 'province' );
			$data ['active_status'] = $this->input->post ( 'active_status' );
			$data ['group'] = $this->input->post ( 'group' );
			$data ['description'] = $this->input->post ( 'description' );
			
			$data ['avatar'] = $this->avatar;
			
			if (! $this->model->add ( $data )) {
				$data ['page_title'] = 'Có lỗi xảy ra';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/visitor/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/visitor">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			} else {
				$data ['page_title'] = 'Thành công';
				$mess_detail = '<p>Thêm mới khách hàng thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/visitor/add">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/visitor">Trở lại trang danh sách</a>';
				$data ['message_detail'] = $mess_detail;
			}
			$data ['main_content'] = 'be/message';
			$this->load->view ( 'be/includes/template', $data );
		}
	}

	function view($id){
		$customer = $this->model->get($id);
				
		$data['id'] = $id;
		$data ['name'] = $customer->name;
		$data ['email'] = $customer->email;
		$data ['phone'] = $customer->phone;
		$data ['address'] = $customer->address;
		$data ['district'] = $customer->district;
		$data ['province'] = $customer->province;
		$data ['active_status'] = $customer->active_status;
		$data ['avatar'] = $customer->avatar;
		$data ['group'] = $customer->group;
		$data ['description'] = $customer->description;
		$data ['page_title'] = 'Thông tin khách hàng';
		$data ['main_content'] = 'be/visitor/detail';
		$this->load->view ( 'be/includes/template', $data );
		
	}

	function get_avatar_uploaded() {
		//Create location if it does not exist
		$location = DEFAULT_IMAGE_UPLOAD_PATH . '/visitor';
		if (! is_dir ( $location )) {
			if (! mkdir ( $location, 0755 )) {
				die ( 'Failed to create folders...' );
			}
		}
		$config ['upload_path'] = $location;
		$config ['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config ['max_size'] = '10000';
		
		$this->load->library ( 'upload', $config );
		if ($this->upload->do_upload ( 'avatar' )) {
			$tmp = $this->upload->data ( 'avatar' );
			$this->avatar = $location . '/' . $tmp ['file_name'];
		} else {
			$this->avatar =  CUSTOMER_DEFAULT_AVATAR;
		}
	}
	
	function get() {
	   
	}

	function get_by_fillter(){
		/*if($_POST['name']==''&& $_POST['province']=='0' && $_POST['district']=='0' && $_POST['status']=='0' && $_POST['group']=='0', $_POST['phone']=='' && $_POST['username']==''){
			redirect(site_url('customer'));
		}
		else{*/
			//$_POST['name'].
			$data ['visitors'] = $this->model->get_by_fillter(trim($_POST['name']), $_POST['province'], $_POST['district'], $_POST['status'], $_POST['group'], trim($_POST['phone']));
			$data ['province'] = $this->model->get_all_province();
			$data ['district'] = $this->model->get_all_district();
			$data ['page_title'] = 'Danh sách khách hàng';
			$data ['main_content'] = 'be/visitor/list';
			$this->load->view ( 'be/includes/template', $data );
		//}
	}
		
	function list_all_visitors() {
		$this->load->library ( 'pagination' );
		$offset = $this->uri->segment ( 2 );
		$limit = 12;
		$config ['base_url'] = site_url ( 'visitor' );
		$config ['uri_segment'] = 2;
		$config ['total_rows'] = $this->db->count_all ( 'visitors' );
		$config ['per_page'] = $limit;
		$this->pagination->initialize ( $config );
		$data ['paginator'] = $this->pagination->create_links ();
		
		$data ['province'] = $this->model->get_all_province();
		$data ['district'] = $this->model->get_all_district();
		$data ['visitors'] = $this->model->get ( 0, $limit, $offset );
		
		$data ['page_title'] = 'Danh sách khách hàng';
		$data ['main_content'] = 'be/visitor/list';
		$this->load->view ( 'be/includes/template', $data );
	}
	
	function searchByProvince() {
		$province = $this->input->post('province');
		$data ['visitors'] = $this->model->searchByProvince($province);
		
		$data ['page_title'] = 'Danh sách khách hàng';
		$data ['main_content'] = 'be/visitor/list';
		$this->load->view ( 'be/includes/template', $data );
	}
	
	function update($id) {		
		
		$visitor = $this->model->get($id);
				
		$data['id'] = $id;
		$data ['name'] = $visitor->name;
		//$data ['username'] = $visitor->username;
		//$data ['password'] = $visitor->password;
		$data ['email'] = $visitor->email;
		$data ['phone'] = $visitor->phone;
		$data ['address'] = $visitor->address;
		$data ['district'] = $visitor->district;
		$data ['province'] = $visitor->province;
		$data ['active_status'] = $visitor->active_status;
		$data ['avatar'] = $visitor->avatar;
		$data ['group'] = $visitor->group;
		$data ['description'] = $visitor->description;
		
		
		$this->form_validation->set_rules ( 'fullname', 'Họ tên' );
		$this->form_validation->set_rules ( 'username', 'Tên tài khoản' );
		$this->form_validation->set_rules ( 'password', 'Mật khẩu' );
		$this->form_validation->set_rules ( 'email', 'Email' );
		$this->form_validation->set_rules ( 'phone', 'Điện thoại' );
		$this->form_validation->set_rules ( 'address', 'Địa chỉ' );
		$this->form_validation->set_rules ( 'district', 'Quận/Huyện' );
		$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố' );
		$this->form_validation->set_rules ( 'active_status', 'Trạng thái' );
		$this->form_validation->set_rules ( 'avatar', 'Hình ảnh', 'callback_get_avatar_uploaded' );
		$this->form_validation->set_rules ( 'group', 'Nhóm khách hàng' );
		$this->form_validation->set_rules ( 'description', 'Mô tả' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['page_title'] = 'Cập nhật thông tin khách hàng';
			$data ['main_content'] = 'be/visitor/update';
			$this->load->view ( 'be/includes/template', $data );
		} else {
			$data ['name'] = $this->input->post ( 'fullname' );
			$data ['username'] = $this->input->post ( 'username' );
			$data ['password'] = $this->input->post ( 'password' );
			$data ['email'] = $this->input->post ( 'email' );
			$data ['phone'] = $this->input->post ( 'phone' );
			$data ['address'] = $this->input->post ( 'address' );
			$data ['district'] = $this->input->post ( 'district' );
			$data ['province'] = $this->input->post ( 'province' );
			$data ['active_status'] = $this->input->post ( 'active_status' );
			$data ['group'] = $this->input->post ( 'group' );
			$data ['description'] = $this->input->post ( 'description' );
			
			if($this->avatar == CUSTOMER_DEFAULT_AVATAR ) {
				$data ['avatar'] = $visitor->avatar;
			}else {
				$data['avatar'] = $this->avatar;
			}					
			if (! $this->model->update ($id, $data )) {
				$data ['page_title'] = 'Có lỗi xảy ra';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/visitor/update"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/visitor">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			} else {
				$data ['page_title'] = 'Thành công';
				$mess_detail = '<p>Cập nhật thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/visitor/update">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/visitor">Trở lại trang danh sách</a>';
				$data ['message_detail'] = $mess_detail;
			}
			$data ['main_content'] = 'be/message';
			$this->load->view ( 'be/includes/template', $data );
		}
	}
	
	function delete() {
		$ids = $this->input->post('selected');
		foreach ($ids as $id){
			$this->model->delete($id);
		}
		redirect(site_url('visitor'));
	}
}